home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK2.toast / Development Kits / Translation Manager / Sample Code / Inside Mac Example (Pascal) / SamplePascalExtension.p next >
Encoding:
Text File  |  1998-06-05  |  9.0 KB  |  262 lines  |  [TEXT/MPS ]

  1. {
  2.     File:        SamplePascalExtension.p 
  3.  
  4.     Contains:    Sample Pascal (how quaint!) file translation extension
  5.  
  6.     Copyright:    © 1991-1993 by Apple Computer, Inc., all rights reserved.
  7. }
  8.  
  9. UNIT SampleExtension;
  10.  
  11. INTERFACE
  12.  
  13.     USES
  14.         MemTypes, Memory, Resources, Files, Errors, Components, TranslationExtensions;
  15.     
  16.     FUNCTION TranslateEntry( VAR params: ComponentParameters; storage: Handle ) : ComponentResult; 
  17.  
  18. IMPLEMENTATION
  19.  
  20.     CONST
  21.         rProgressAdvertismentResID     = 150;
  22.                 
  23.     FUNCTION DoGetFileTranslationList(self                   : ComponentInstance; 
  24.                                       translationList     : FileTranslationListHandle):ComponentResult; FORWARD;
  25.                                       
  26.     FUNCTION DoIdentifyFile(self                : ComponentInstance; 
  27.                             targetDocument        : FSSpec; 
  28.                         VAR documentIdentfiedAs    : FileType) :ComponentResult; FORWARD;
  29.     
  30.     FUNCTION DoTranslateFile(self                            : ComponentInstance; 
  31.                              refNum                            : TranslationRefNum;
  32.                              sourceDocument                    : FSSpec; 
  33.                              sourceDocumentType                : FileType; 
  34.                              sourceDocumentTypeHint            : LONGINT; 
  35.                              destinationDocument            : FSSpec; 
  36.                              destinationDocumentType        : FileType; 
  37.                              destinationDocumentTypeHint    : LONGINT): ComponentResult; FORWARD;
  38.  
  39.  
  40. {_______________________________________________________________________________________________}
  41. {                                                                                                }
  42. { TranslateEntry                                                                                }
  43. {                                                                                                }
  44. { This is the main entry point for the extension.  It handles some Component Manager setup, but    }
  45. { it's main task is to dispatch to the extensions subroutines when known selectors are            }
  46. { encountered.                                                                                    }
  47. {                                                                                                }
  48. FUNCTION TranslateEntry( VAR params: ComponentParameters; storage: Handle ) :ComponentResult;
  49. TYPE
  50.     LongHandle = ^LongPtr;
  51.     LongPtr    = ^Longint;
  52. VAR
  53.     self:         ComponentInstance;
  54.     h:            Handle;
  55.     selector:    INTEGER;
  56. BEGIN
  57.     
  58.     CASE (params.what) OF
  59.     
  60.         { Handle the Component Manager opening up this component }
  61.         kComponentOpenSelect:
  62.             BEGIN
  63.                 self := ComponentInstance(params.params[0]);
  64.                 h := NewHandle(sizeof(ComponentInstance));              { allocate storage }
  65.                 IF h <> nil THEN
  66.                     BEGIN
  67.                         LongHandle(h)^^ := ORD4(self);                              { put instance in storage }
  68.                         SetComponentInstanceStorage(self, h);
  69.                         TranslateEntry := noErr;
  70.                     END
  71.                 ELSE
  72.                     TranslateEntry := MemError;
  73.             END;
  74.  
  75.         { Compoenent Manager is closing us down, clean up }
  76.         kComponentCloseSelect:
  77.             BEGIN
  78.                 DisposeHandle(storage);
  79.                 TranslateEntry := noErr;
  80.             END;
  81.  
  82.         { Compoenent Manager needs to know what selectors we can handle }
  83.         kComponentCanDoSelect:
  84.             BEGIN
  85.                 selector := INTEGER((Ptr(params.params)^));
  86.     
  87.                 IF ( ((kComponentVersionSelect <= selector) AND (selector <= kComponentOpenSelect))
  88.                   OR ((kTranslateGetFileTranslationList <= selector) AND (selector <= kTranslateTranslateFile)) ) THEN
  89.                       TranslateEntry := 1
  90.                 ELSE
  91.                     TranslateEntry := 0;
  92.             END;
  93.         
  94.         { No version stuff }
  95.         kComponentVersionSelect:            
  96.             TranslateEntry := noErr;
  97.                 
  98.         { Want file translation list? }
  99.         kTranslateGetFileTranslationList:
  100.             TranslateEntry := CallComponentFunctionWithStorage(Handle(storage^^), params, ComponentFunction(@DoGetFileTranslationList));
  101.  
  102.         { Want to identify file? }
  103.         kTranslateIdentifyFile:        
  104.             TranslateEntry := CallComponentFunctionWithStorage(Handle(storage^^), params, ComponentFunction(@DoIdentifyFile));
  105.  
  106.         { Want to translate file? }
  107.         kTranslateTranslateFile:            
  108.             TranslateEntry := CallComponentFunctionWithStorage(Handle(storage^^), params, ComponentFunction(@DoTranslateFile));
  109.         
  110.         { No, idea, tell the Component Manager that }
  111.         OTHERWISE
  112.             TranslateEntry := badComponentSelector;
  113.     END;    { CASE }
  114. END;
  115.  
  116.  
  117.     
  118.  
  119. {_______________________________________________________________________________________________}
  120. {                                                                                                }
  121. { DoGetFileTranslationList                                                                        }
  122. {                                                                                                }
  123. { This routine fills out a file translation list for this extension.  Let's assume that this    }
  124. { extension can do the following translations:                                                    }
  125. {                                                                                                }
  126. { FROM:                            TO:                                                                }
  127. { _________________             ________________                                                }
  128. { SleepyWrite (slpy)            TeachText(ttro)                                                    }
  129. { HappyWrite  (hapy)                                                                            }
  130. {                                                                                                }
  131. FUNCTION DoGetFileTranslationList(self                   : ComponentInstance; 
  132.                                   translationList     : FileTranslationListHandle) : ComponentResult;
  133. CONST
  134.     kStamp    = $A74520A8; {time this code was compiled}
  135. TYPE
  136.   MyList = RECORD
  137.             modDate:            LONGINT;
  138.             groupCount:         LONGINT;
  139.             group1SrcCount:     LONGINT; 
  140.             group1SrcEntrySize: LONGINT;
  141.             group1SrcTypes:     ARRAY[1..2] OF FileTypeSpec; 
  142.             group1DstCount:     LONGINT;
  143.             group1DstEntrySize: LONGINT;
  144.             group1DstTypes:        ARRAY[1..1] OF FileTypeSpec;
  145.           END;
  146.     MyListPtr    = ^MyList;
  147.     MyListHandle = ^MyListPtr;
  148. VAR
  149.     result         : OSErr;
  150.     listPtr        : MyListPtr;
  151. BEGIN    
  152.     result := noErr;
  153.     
  154.     { This extension has a "hard-coded" list of translations - it never changes.  The first }
  155.     { time Gremlin EVER calls this extension fill out the list, then every other time it     }
  156.     { passes us the list back, just pass it back to 'em without looking - things haven't    }
  157.     { changed.                                                                                }
  158.     IF translationList^^.modDate <> kStamp THEN
  159.     BEGIN
  160.         { Adjust the handle so there's enough room for all the data we're going to stuff }            
  161.         SetHandleSize( Handle(translationList), sizeof(MyList) );
  162.         result := MemError;
  163.         IF result = noErr THEN
  164.         BEGIN
  165.             listPtr := MyListHandle(translationList)^;
  166.             WITH listPtr^ DO
  167.             BEGIN
  168.                 { See we know we've done this before }
  169.                 modDate := kStamp;
  170.                 
  171.                 { We've got one group }
  172.                 groupCount := 1;
  173.              
  174.                 { src side has two entries }
  175.                 group1SrcCount := 2;
  176.                 group1SrcEntrySize := SizeOf(FileTypeSpec);
  177.                 group1SrcTypes[1].format := 'slpy';                        { SleepyWrite document format }
  178.                 group1SrcTypes[1].hint := 0;                            { No hint }
  179.                 group1SrcTypes[1].flags := 0;                            { No specials }
  180.                 group1SrcTypes[1].catInfoType := 'slpy';                { Catalog type }
  181.                 group1SrcTypes[1].catInfoCreator := 'dsny';                { Catalog creator }
  182.                 group1SrcTypes[2].format := 'hapy';                        { HappyWrite document format }
  183.                 group1SrcTypes[2].hint := 0;                            { No hint }
  184.                 group1SrcTypes[2].flags := 0;                            { No specials }
  185.                 group1SrcTypes[2].catInfoType := 'hapy';                { Catalog type }
  186.                 group1SrcTypes[2].catInfoCreator := 'mcky';                { Catalog creator }
  187.     
  188.                 { dst side has one entry }
  189.                 group1DstCount := 1;
  190.                 group1DstEntrySize := SizeOf(FileTypeSpec);
  191.                 group1DstTypes[1].format := 'ttro';                        { TeachText document format }
  192.                 group1DstTypes[1].hint := 0;                            { No hint }
  193.                 group1DstTypes[1].flags := taDstDocNeedsResourceFork;    { TeachText documents have resource forks where they store they're pictures }
  194.                 group1DstTypes[1].catInfoType := 'ttro';                { Catalog type }
  195.                 group1DstTypes[1].catInfoCreator := 'ttxt';                { Catalog creator }
  196.             END ; {with}
  197.         END; {if}
  198.     END; {if}
  199.  
  200.     DoGetFileTranslationList := result;
  201. END;
  202.  
  203. {_______________________________________________________________________________________________}
  204. {                                                                                                }
  205. { DoIdentifyFile                                                                                }
  206. {                                                                                                }
  207. { This routine is responsible for identifing a file.  The actual identification code is not     }
  208. { here, just a shell (sorry).                                                                    }
  209. {                                                                                                }
  210. FUNCTION DoIdentifyFile(self                : ComponentInstance; 
  211.                         targetDocument        : FSSpec; 
  212.                     VAR documentIdentfiedAs    : FileType) :ComponentResult; 
  213. VAR
  214.     identified : Boolean;
  215. BEGIN
  216.     { Use our mystery routine to identify the file }
  217. {    identified := IdentifyDocument(targetDocument, documentIdentfiedAs);    }
  218.  
  219.     IF identified THEN
  220.         DoIdentifyFile := noErr
  221.     ELSE
  222.         DoIdentifyFile := noTypeErr;
  223. END;
  224.  
  225.  
  226. {_______________________________________________________________________________________________}
  227. {                                                                                                }
  228. { DoTranslateFile                                                                                }
  229. {                                                                                                }
  230. { This routine is responsible for tranlating a file.  The actual identification code is not     }
  231. { here, just a shell (sorry).                                                                    }
  232. {                                                                                                }
  233. FUNCTION DoTranslateFile(self                            : ComponentInstance; 
  234.                          refNum                            : TranslationRefNum;
  235.                           sourceDocument                    : FSSpec; 
  236.                          sourceDocumentType                : FileType; 
  237.                          sourceDocumentTypeHint            : LONGINT; 
  238.                             destinationDocument            : FSSpec; 
  239.                          destinationDocumentType        : FileType; 
  240.                          destinationDocumentTypeHint    : LONGINT): ComponentResult; 
  241. VAR
  242.         advert:        Handle;
  243.         myResFile:     INTEGER;
  244.         result:        OSErr;
  245. BEGIN
  246.     { first thing to do is display progress dialog and show advertisement }
  247.     myResFile := OpenComponentResFile(Component(self));
  248.     
  249.     IF (myResFile <> -1) THEN
  250.         BEGIN
  251.             advert := GetResource('PICT', rProgressAdvertismentResID);
  252.             DetachResource(advert);
  253.             result := SetTranslationAdvertisement(refNum, PicHandle(advert));
  254.             result := CloseComponentResFile(myResFile);
  255.         END;
  256.  
  257.     { Now call the magic routine to translate the file }
  258. {    DoTranslateFile := PerformTranslation(refNum, sourceDocument, sourceDocumentType, destinationDocument,destinationDocumentType);}
  259. END;
  260.  
  261.  
  262. END.